package com.lagou.edu.controller;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */


    @Autowired
    private ResumeDao resumeDao;

    @RequestMapping("/queryAll")
    public ModelAndView queryAll() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> list= resumeDao.findAll();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("table");
        return modelAndView;
    }

    @RequestMapping("/save")
    public String save(Resume resume) throws Exception {
        try {
            resume = resumeDao.save(resume);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:queryAll";
    }

    @RequestMapping("/edit")
    public String edit( Resume resume) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(resume);
        try {
             resumeDao.save(resume);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map map=new HashMap();
        map.put("success",true);
        modelAndView.setViewName("table");

        return "redirect:queryAll";
    }


    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Map delete( @PathVariable("id") Integer id) throws Exception {
        System.out.println(id);
        Resume resume=new Resume();
        resume.setId(id);
        try {
            resumeDao.delete(resume);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map map=new HashMap();
        map.put("success",true);
        return map;
    }
}
